package com.atpy.reggie.controller;

import com.atpy.reggie.common.R;
import com.atpy.reggie.dto.DishDto;
import com.atpy.reggie.entity.Dish;
import com.atpy.reggie.entity.DishFlavor;
import com.atpy.reggie.service.CategoryService;
import com.atpy.reggie.service.DishFlavorService;
import com.atpy.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    public R<String> Save(@RequestBody DishDto dishDto){

        final Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
         log.info(dishDto.toString());
         dishService.saveWithFlavor(dishDto);
        return R.success("新增菜品成功");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        Page<Dish> dishPage=new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.like(name!=null,Dish::getName,name);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage, dishLambdaQueryWrapper);
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");

        final val records = dishPage.getRecords();
        List<DishDto> list = records.stream().map((item)->{
            final val dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            final val categoryId = item.getCategoryId();
            final val category = categoryService.getById(categoryId);
            if (category!=null){
                final val categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable("id") long id){
        final val dishDto = dishService.getByidWithFlavor(id);


        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
//        String key="dish_"+dishDto.getCategoryId()+"_1";
//        redisTemplate.delete(key);
        final Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }
    @PostMapping("/status/{status}")
    public R<String> update(@PathVariable("status") int status, @RequestParam("ids") List<Long> ids){

        final Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        log.info(ids.toString());
        final val dishList = dishService.listByIds(ids);
        List<Dish> list=dishList.stream().map((item)->{
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        dishService.updateBatchById(list);
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") List<Long> ids){
        final Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        dishService.removeByIds(ids);

        return R.success("删除成功");
    }


//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper= new LambdaQueryWrapper<>();
//        dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
//        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        final val dishList = dishService.list(dishLambdaQueryWrapper);
//        return R.success(dishList);
//    }
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
        List<DishDto> list=null;
        String key="dish_"+dish.getCategoryId()+"_"+dish.getStatus();
        list = (List<DishDto>) redisTemplate.opsForValue().get(key);
        if (list!=null){
            return R.success(list);
        }

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper= new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        dishLambdaQueryWrapper.eq(Dish::getStatus,1);
        dishLambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        final val dishList = dishService.list(dishLambdaQueryWrapper);
        list=dishList.stream().map((item)->{
            final val dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            final val dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper=new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            final val dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            final val categoryId = item.getCategoryId();
            final val category = categoryService.getById(categoryId);
            final val name = category.getName();
            dishDto.setCategoryName(name);
            return dishDto;
        }).collect(Collectors.toList());
        redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
        return R.success(list);
    }

}
